package com.wy.powerbuilder.controller.ld;

import com.github.pagehelper.PageInfo;
import com.wy.powerbuilder.entity.ld.Menu;
import com.wy.powerbuilder.entity.ld.Role;
import com.wy.powerbuilder.entity.ld.Sys_role_menu;
import com.wy.powerbuilder.entity.ld.User;
import com.wy.powerbuilder.service.ld.MenuService;
import com.wy.powerbuilder.service.ld.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @RequestMapping("role")
    public String role(Model model,Integer pageNo){
        PageInfo<Role> pageInfo=roleService.pageAll(pageNo);
        model.addAttribute("pageInfo",pageInfo);
        return "admin-role";
    }
    @RequestMapping("showMenu")
    public String showMenu(Model model,int rid){
        List<Menu> menus=menuService.getMenus(rid);
        model.addAttribute("menus",menus);
        return "showMenu";
    }
    @RequestMapping("showAddrole")
    public String showAddrole(){
        return "admin-role-add";
    }
    @ResponseBody
    @RequestMapping("addRole")
    public String addRole(String[] mids,String r_name,String r_remark){
        Role role=new Role();
        role.setR_name(r_name);
        role.setR_remark(r_remark);
        roleService.addRole(role);
        int r_id=roleService.getRole(r_name).getR_id();
        for(int i=0;i<mids.length;i++){
            Sys_role_menu sys_role_menu=new Sys_role_menu();
            sys_role_menu.setRid(r_id);
            sys_role_menu.setMid(Integer.parseInt(mids[i]));
            menuService.addRole_menu(sys_role_menu);
        }
        System.out.println(r_id);
        return "";
    }
    @ResponseBody
    @RequestMapping("roleName")
    public String roleName(String name){
        System.out.println(name);
        Role role=roleService.getRole(name);
        if(role==null){
            return "yes";
        }
        return "no";
    }
    @RequestMapping("delRole")
    public String delRole(int r_id){
        menuService.delRole_menu(r_id);
        int i=roleService.delRole(r_id);
        return "redirect:role";
    }
    @RequestMapping("showUpdaterole")
    public String showUpdaterole(int r_id,Model model){
        Role role=roleService.showRole(r_id);
        List<Sys_role_menu> list=menuService.getRole_menus(r_id);
        model.addAttribute("list",list);
        model.addAttribute("role",role);
        return "admin-role-update";
    }
    @ResponseBody
    @RequestMapping("updateRole")
    public String updateRole(String[] mids,String r_name,String r_remark,int r_id){
        Role role=new Role();
        role.setR_id(r_id);
        role.setR_name(r_name);
        role.setR_remark(r_remark);
        roleService.updateRole(role);
        menuService.delRole_menu(r_id);
        for(int i=0;i<mids.length;i++){
            Sys_role_menu sys_role_menu=new Sys_role_menu();
            sys_role_menu.setRid(r_id);
            sys_role_menu.setMid(Integer.parseInt(mids[i]));
            menuService.addRole_menu(sys_role_menu);
        }
        return "";
    }
}
